<template>
	<view class="content">
		<uv-navbar title="" :placeholder="true" @leftClick="leftClick"></uv-navbar>
		<view class="content_box">
			<view class="content_box_title">
				重设密码
			</view>
			<view class="content_box_input">
				<view class="name">手机号</view>
				<input type="text" v-model="shouji" placeholder="请输入手机号"/>
			</view>
			<view class="content_box_input">
				<view class="name">图形验证码</view>
				<input type="text" v-model="validdatecode" placeholder="请输入图形验证码"/>
				<image mode="widthFix" class="yzm" @click="getCode" :src="'data:image/png;base64,'+imgCode"></image>
			</view>
			<view class="content_box_input">
				<view class="name">短信验证码</view>
				<input type="text" v-model="mobileCode" placeholder="请输入短信验证码"/>
				<view class="getyzm" @click="getWrap">{{iswrap?'获取验证码':`${wrapTime}秒重新获取`}}</view>
			</view>
			<view class="content_box_input">
				<view class="name">新密码</view>
				<input type="text" v-model="newpass" :password="true" placeholder="请输入新密码"/>
			</view>
			<view class="content_box_button" @click="tijiao">
				完成并登录
			</view>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		onMounted
	} from 'vue'
	import {
		homeApi
	} from "@/api/index"
	import {
		isPhone,
		isEmail,
		link
	} from "@/utils/utils.js"
	onMounted((open) => {
		getCode()
	})
	// 获取验证
	const imgCode = ref('')
	const validkey = ref('')
	const getCode = async () => {
		let data = {
			do: "verifycode2",
			sizemode: 2,
		}
		let res = await homeApi.publicGet(data)
		imgCode.value = res.columns[0].imagedata
		validkey.value = res.columns[0].validkey2
	}
	// 验证码倒计时
	const wrapTime = ref(60)
	const iswrap = ref(true)
	const getWrap = () => {
		if (iswrap.value) {
			getTelCode()
		}
	}
	const getTelCode = async () => {
		if (shouji.value == '') {
			uni.showToast({
				title: '请输入手机号',
				icon: 'none',
				duration: 2000
			});
			return
		}
		if (!isPhone(shouji.value)) {
			uni.showToast({
				title: '请输入正确的手机号',
				icon: 'none',
				duration: 2000
			});
			return
		}
		let data = {
			SendToPhone: shouji.value,
			validdate: validdatecode.value,
			validkey2: validkey.value
		}
		let res = await homeApi.publicPost(data, 'sendmobilecode')
		if (res.columns[0].error_no == '0') {
			iswrap.value = false
			wrapTime.value = 60
			uni.showToast({
				title: res.columns[0].error_msg,
				icon: 'none',
				duration: 2000
			});
			const countdownId = setInterval(() => {
				if (wrapTime.value > 0) {
					wrapTime.value--;
				} else {
					clearInterval(countdownId);
					iswrap.value = true
				}
			}, 1000)
		} else {
			iswrap.value = true
			uni.showToast({
				title: res.columns[0].error_msg,
				icon: 'none',
				duration: 2000
			});
		}
	}
	const shouji = ref('')
	const newpass = ref('')
	const mobileCode = ref('')
	const validdatecode = ref('')
	const tijiao = async () => {
		let data = {
			shouji:shouji.value,
			newpass:newpass.value,
			validkey2:validkey.value,
			validdate:validdatecode.value,
			MobileCode:mobileCode.value
		}
		let res = await homeApi.publicPost(data, 'resetpassbymobile')
		if (res.statuscode == '200') {
			uni.showToast({
				title: res.error_msg,
				icon: 'none',
				duration: 2000
			});
			setTimeout(() => {
				uni.navigateBack({
					delta: 3
				})
			}, 2000)
		}else{
			uni.showToast({
				title: res.error_msg,
				icon: 'none',
				duration: 2000
			});
		}
	}
	
	const leftClick = () => {
		uni.navigateBack({
			delta: 1
		})
	}
</script>

<style lang="scss" scoped>
	.content {
		width: 100%;
		height: 100%;
		display: flex;
		flex-direction: column;
	
		&_box {
			width: 100%;
			flex: 1;
			display: flex;
			flex-direction: column;
			align-items: center;
			&_title{
				font-size:42rpx;
				font-weight: bold;
				margin-top: 5vh;
				padding-bottom: 7vh;
			}
			&_input{
				display: flex;
				align-items: center;
				width: 90%;
				border-bottom: 1px solid #dededf;
				height: 90rpx;
				margin-top: 30rpx;
				.name{
					font-size: 32rpx;
					width: 190rpx;
				}
				input{
					flex: 1;
					height: 90rpx;
					font-size: 30rpx;
					margin-right: 20rpx;
				}
				.getyzm{
					padding: 10rpx 20rpx;
					border: 1px solid #e94f06;
					border-radius: 30px;
					font-size: 30rpx;
					color: #e94f06;
				}
				.yzm{
					width: 200rpx;
				}
			}
			&_text{
				width: 90%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				font-size: 30rpx;
				color: #e94f06;
				margin-top: 20rpx;
			}
			&_button{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				color: #fff;
				background: #e94f06;
				margin-top: 60rpx;
				font-size: 36rpx;
				border-radius: 30px;
			}
			&_zc{
				width: 90%;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 36rpx;
			}
		}
	}
</style>